package it.traveldream.worker.beans;




import it.traveldreamClient.auxiliary.Input;
import it.traveldreamClient.beans.ComponentiMgrBeanInterface;
import it.traveldreamClient.beans.dto.AlloggioDTO;
import it.traveldreamClient.beans.dto.ComponenteDTO;
import it.traveldreamClient.beans.dto.ComponenteNuovoTipoTrasportoDTO;
import it.traveldreamClient.beans.dto.EscursioneDTO;
import it.traveldreamClient.beans.dto.NuovoTipoTrasportoDTO;
import it.traveldreamClient.beans.dto.TrasportoDTO;
import it.traveldreamClient.beans.dto.VoloDTO;

import java.sql.Date;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

@ManagedBean(name="tipicomponente")
@SessionScoped
public class TipiComponenteBean {
	@EJB
	private ComponentiMgrBeanInterface pkg;

	private VoloDTO vDTO;
	private AlloggioDTO aDTO;
	private EscursioneDTO eDTO;
	private ComponenteDTO compDTO;
	private TrasportoDTO tDTO;
	private ArrayList<ComponenteDTO> listcomp;
	private ComponenteNuovoTipoTrasportoDTO tntDTO;
	private List<String> Tipi;
	private String tipostr, idDaEliminare, tipoel;
	private String text;  
	private Integer integerAll, integerEsc, integerVolo, integerTNT, integerEscdur;
	private List<String> radioButtons;
	private List<String> checkBoxes;
	private List<Input> textAreas;
	private boolean t=false;
	      
	java.util.Calendar cal = java.util.Calendar.getInstance();
	java.util.Date utilDate = cal.getTime();
	java.sql.Date oggi = new Date(utilDate.getTime());
	
	public TipiComponenteBean(){
		vDTO=new VoloDTO();
		aDTO=new AlloggioDTO();
		eDTO=new EscursioneDTO();
		tntDTO=new ComponenteNuovoTipoTrasportoDTO();
		Tipi=new ArrayList<String>();
		listcomp=new ArrayList<ComponenteDTO>();
	}
	
	@PostConstruct
	public void init(){
	}
	public VoloDTO getvDTO() {
		return vDTO;
	}
	public void setvDTO(VoloDTO vDTO) {
		this.vDTO = vDTO;
	}
	public AlloggioDTO getaDTO() {
		return aDTO;
	}
	public void setaDTO(AlloggioDTO aDTO) {
		this.aDTO = aDTO;
	}
	public EscursioneDTO geteDTO() {
		return eDTO;
	}
	public void seteDTO(EscursioneDTO eDTO) {
		this.eDTO = eDTO;
	}
	public ComponenteNuovoTipoTrasportoDTO getTntDTO() {
		return tntDTO;
	}
	public void setTntDTO(ComponenteNuovoTipoTrasportoDTO tntDTO) {
		this.tntDTO = tntDTO;
	}
	
	
	public List<String> getRadioButtons() {
		return radioButtons;
	}

	public void setRadioButtons(List<String> radioButtons) {
		this.radioButtons = radioButtons;
	}

	public List<String> getCheckBoxes() {
		return checkBoxes;
	}

	public void setCheckBoxes(List<String> checkBoxes) {
		this.checkBoxes = checkBoxes;
	}

	public List<Input> getTextAreas() {
		return textAreas;
	}

	public void setTextAreas(List<Input> textAreas) {
		this.textAreas = textAreas;
	}

	public List<String> getTipi() {
		List<NuovoTipoTrasportoDTO> lista=pkg.getTipi();
		for(NuovoTipoTrasportoDTO t: lista){
			Tipi.add(t.getNome());
		}
		return Tipi;
	}
	public void setTipi(ArrayList<String> tipi) {
		Tipi = tipi;
	}
	public String getTipostr() {
		return tipostr;
	}
	public Date oggi(){
		return oggi;
	}
	public void setTipostr(String tipostr) {
		this.tipostr = tipostr;
	}
	public String changep() {
		if(tipostr.equals("Volo")||tipostr.equals("Alloggio")||(tipostr.equals("Escursione"))){
			return "/employ/"+tipostr+"?faces-redirect=true";
		}
		NuovoTipoTrasportoDTO dto=pkg.getTipo(tipostr);
		System.out.println(tipostr+"SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS"+dto.toString());
		radioButtons=new LinkedList<String>();
		checkBoxes=new LinkedList<String>();
		textAreas=new LinkedList<Input>();
		tntDTO.setTipoDTO(dto);
		for (String s: dto.getOpzioniRadioButton()) {
			radioButtons.add(s);
			
		}
		for (String s: dto.getLabelTextAreas()) {
			textAreas.add(new Input(s));
			
		}
		
		for (String s: dto.getOpzioniCheckBoxes()) {
			checkBoxes.add(s);

		}
		
		
		return "/employ/creaComponenteNuovoTipo?faces-redirect=true";
	}
	
	public String saveAlloggio() {
		if(t==true){
			return null;
		}
    	pkg.saveAlloggio(aDTO);
    	return "/employ/index?faces-redirect=true";
	}
	public String saveEscursione() {
		if(t==true){
			return null;
		}
    	pkg.saveEscursione(eDTO);
    	return "/employ/index?faces-redirect=true";
	} 
	public void CheckNegativoEsc(){
		if(Integer.parseInt(eDTO.getPrezzo())<=0){
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Errore", "Prezzo Negativo"));  
			t=true;
		}
		else if(Integer.parseInt(eDTO.getDurata())<=0){
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Errore", "Durata Negativa"));  
			t=true;
		}
		else
		 t=false;
	}
	public void checkDataAlloggio(ActionEvent actionEvent){
			if((aDTO.getCheckOut().before(aDTO.getCheckIn()))){
				FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Errore", "Date di Check-in e Check-out inconsistenti "));  
				t=true;
			}
			else{
				t=false;
			}
			if(Integer.parseInt(aDTO.getPrezzo())<=0){
				FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Errore", "Prezzo Negativo"));  
				t=true;
			}
	}
	public void checkDataVolo(ActionEvent actionEvent){
		if((vDTO.getDataArrivo().before(vDTO.getDataPartenza()))){
				FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Errore", "Date di Partenza-in e Arrivo inconsistenti "));  
				t=true;
			}
			else{
				t=false;
			}
		if(Integer.parseInt(vDTO.getPrezzo())<=0){
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Errore", "Prezzo Negativo"));  
			t=true;
		}
	}
	public void checkDataTnt(ActionEvent actionEvent){
			if((tntDTO.getDataArrivo().before(tntDTO.getDataPartenza()))){
				FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Errore", "Date non Corrette"));  
				t=true;
			}
			else{
				t=false;
			}
			if(Integer.parseInt(tntDTO.getPrezzo())<=0){
				FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Errore", "Prezzo Negativo"));  
				t=true;
			}
	}
	public String saveVolo() {
		if(t==true){
			return null;
		}
    	pkg.saveVolo(vDTO);
    	return "/employ/index?faces-redirect=true";
	}
	
	public String saveComponenteNuovoTipo(){
		for(Input in: textAreas) {
			tntDTO.getValoriDaTextAreas().add(in.getValue());
		}
		if(t==true){
			return null;
		}
		pkg.saveComponenteNuovoTipo(tntDTO);
		return "/employ/index?faces-redirect=true";
	}
	
	public String getIdDaEliminare() {
		return idDaEliminare;
	}
	public void setIdDaEliminare(String idDaEliminare) {
		this.idDaEliminare = idDaEliminare;
	}
	
	public String riempilistchp(){
		if(tipoel.equals("Alloggio")){
			List<AlloggioDTO> al=pkg.getAllAlloggi();
			for(int i=0;i<al.size();i++)
				listcomp.add(al.get(i));
		}
		if(tipoel.equals("Escursione")){
			List<EscursioneDTO> al=pkg.getAllEscursioni();
			for(int i=0;i<al.size();i++)
				listcomp.add(al.get(i));
		}
		if(tipoel.equals("Trasporto")){
			List<TrasportoDTO> al=pkg.getAllTrasporti();
			for(int i=0;i<al.size();i++)
				listcomp.add(al.get(i));
		}
		return "/employ/tabEliminaComp?faces-redirect=true";
	}
	public String getTipoel() {
		return tipoel;
	}

	public void setTipoel(String tipoel) {
		this.tipoel = tipoel;
	}

	public ArrayList<ComponenteDTO> getListcomp() {
		return listcomp;
	}

	public void setListcomp(ArrayList<ComponenteDTO> listcomp) {
		this.listcomp = listcomp;
	}

	public ComponenteDTO getCompDTO() {
		return compDTO;
	}

	public void setCompDTO(ComponenteDTO compDTO) {
		this.compDTO = compDTO;
	}
	
	public String getText() {  
		return text;  
	}  
	public void setText(String text) {  
		this.text = text;  
	}

	public Integer getIntegerVolo() {
		return integerVolo;
	}

	public void setIntegerVolo(Integer integerVolo) {
		this.integerVolo = integerVolo;
		vDTO.setPrezzo(String.valueOf(integerVolo));

	}

	public Integer getIntegerAll() {
		return integerAll;
	}

	public void setIntegerAll(Integer integerAll) {
		this.integerAll = integerAll;
		aDTO.setPrezzo(String.valueOf(integerAll));
	}

	public Integer getIntegerEsc() {
		return integerEsc;
	}

	public void setIntegerEsc(Integer integerEsc) {
		this.integerEsc = integerEsc;
		eDTO.setPrezzo(String.valueOf(integerEsc));

	}

	public Integer getIntegerTNT() {
		return integerTNT;
	}

	public void setIntegerTNT(Integer integerTNT) {
		this.integerTNT = integerTNT;
		tntDTO.setPrezzo(String.valueOf(integerTNT));

	}

	public Integer getIntegerEscdur() {
		return integerEscdur;
	}

	public void setIntegerEscdur(Integer integerEscdur) {
		this.integerEscdur = integerEscdur;
		eDTO.setDurata(String.valueOf(integerEscdur));

	}  
	
}